import { postUserLogin, postCode } from '@/api/user.js'
export default {
  namespaced: true,
  state() {
    return {
      // 用户信息
      profile: {
        id: null,
        account: null,
        avatar: null,
        nickname: null, // 用户名
        mobile: null, // 手机号
        token: null
      }
    }
  },
  mutations: {
    setProfile(state, obj) {
      state.profile = obj
    },
    deleteProfile(state) {
      state.profile.id = null
      state.profile.account = null
      state.profile.avatar = null
      state.profile.nickname = null
      state.profile.mobile = null
      state.profile.token = null
    }
  },
  actions: {
    async getUserDate(ctx, val) {
      let data = null
      try {
        if (val.account) {
          data = await postUserLogin(val)
        } else {
          data = await postCode(val)
        }
        const { id, account, mobile, avatar, nickname, token } = data.result
        const user = { id, account, mobile, avatar, nickname, token }
        ctx.commit('setProfile', user)
        const e = true
        const text = '登录成功'
        return { e, text }
      } catch (err) {
        const e = false
        const text = err.response.data.message
        return { e, text }
      }
    }
  }
}
